package oa.hleast.xswl.controller.app.pms;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.common.enums.MarketingFieldTypeEnum;
import oa.hleast.xswl.pojo.domain.pms.PmsMarketingField;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import oa.hleast.xswl.pojo.dto.pms.SkuDTO;
import oa.hleast.xswl.service.pms.IPmsSkuService;
import oa.hleast.xswl.service.pms.IPmsSpuService;
import oa.hleast.xswl.service.pms.PmsMarketingFieldService;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "【移动端】营销场管理表")
@RestController("AppPmsMarketingFieldController")
@RequestMapping("/api.app/v1/marketingFields")
@Slf4j
@AllArgsConstructor
public class PmsMarketingFieldController {

    private PmsMarketingFieldService pmsMarketingFieldService;
    private IPmsSkuService pmsSkuService;
    private IPmsSpuService pmsSpuService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "类型", required = true, paramType = "query", dataType = "MarketingFieldTypeEnum", dataTypeClass = MarketingFieldTypeEnum.class),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping
    public Result<List<PmsMarketingField>> list(
            String queryMode,
            Integer page,
            Integer limit,
            @RequestParam(value="type") Integer type,
            String name) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<PmsMarketingField> queryWrapper = new LambdaQueryWrapper<PmsMarketingField>()
                        .like(StrUtil.isNotBlank(name), PmsMarketingField::getName, name)
                        .eq(PmsMarketingField::getType, type)
                        .eq(PmsMarketingField::getStatus, 1)
                        .ge(PmsMarketingField::getEnd, new Date())
                        .orderByAsc(PmsMarketingField::getSort)
                        .orderByDesc(PmsMarketingField::getGmtModified)
                        .orderByDesc(PmsMarketingField::getGmtCreate);

                Page<PmsMarketingField> result = pmsMarketingFieldService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result<PmsMarketingField> detail(@PathVariable Long id) {
        PmsMarketingField pmsMarketingField = pmsMarketingFieldService.getById(id);
        SkuDTO skuDTO = pmsSkuService.getSkuById(pmsMarketingField.getSkuId());
        PmsSpu pmsSpu = pmsSpuService.getById(pmsMarketingField.getSpuId());
        pmsMarketingField.setSkuDTO(skuDTO);
        pmsMarketingField.setPmsSpu(pmsSpu);
        return Result.success(pmsMarketingField);
    }


    @ApiOperation(value = "新增1人数和金额", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    })
    @PostMapping("/addBuyNumsAndMoney/{id}")
    public Result addBuyNumsAndMoney(@PathVariable Long id) {
        LambdaUpdateWrapper<PmsMarketingField> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.setSql("cur_buy_nums=cur_buy_nums+1");
        lambdaUpdateWrapper.setSql("cur_buy_prices=cur_buy_prices+price");
        lambdaUpdateWrapper.eq(PmsMarketingField::getId, id);
        boolean status = pmsMarketingFieldService.update(lambdaUpdateWrapper);
        PmsMarketingField pmsMarketingField = pmsMarketingFieldService.getById(id);
        return Result.judge(status);
    }

    @ApiOperation(value = "释放1人数和金额", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    })
    @PostMapping("/minusBuyNumsAndMoney/{id}")
    public Result minusBuyNumsAndMoney(@PathVariable Long id) {
        LambdaUpdateWrapper<PmsMarketingField> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.setSql("cur_buy_nums=cur_buy_nums-1");
        lambdaUpdateWrapper.setSql("cur_buy_prices=cur_buy_prices-price");
        lambdaUpdateWrapper.eq(PmsMarketingField::getId, id);
        boolean status = pmsMarketingFieldService.update(lambdaUpdateWrapper);
        return Result.judge(status);
    }

}
